Page({
  data: {
    searchValue: '',
    selectedProduct: '',
    selectedRegion: '',
    outletList: [],
    allOutlets: [ // 模拟的网点数据
      { id: 1, name: '美的官方维修点-天河区', address: '广州市天河区华南理工大学内', phone: '13800138000', latitude: 23.13, longitude: 113.32, product: '空调', region: '广州市天河区' },
      { id: 2, name: '美的官方维修点-越秀区', address: '广州市越秀区农讲所附近', phone: '13912345678', latitude: 23.13, longitude: 113.26, product: '冰箱', region: '广州市越秀区' },
      { id: 3, name: '格力特约维修点-海珠区', address: '广州市海珠区客村', phone: '13787654321', latitude: 23.09, longitude: 113.32, product: '空调', region: '广州市海珠区' },
      { id: 4, name: '海尔服务中心-番禺区', address: '广州市番禺区大学城', phone: '13654321098', latitude: 23.05, longitude: 113.38, product: '洗衣机', region: '广州市番禺区' },
      { id: 5, name: '美的官方维修点-南山区', address: '深圳市南山区高新园', phone: '13511223344', latitude: 22.54, longitude: 113.94, product: '空调', region: '深圳市南山区' },
    ]
  },
  onLoad() {
    this.autoLocate();
    // 监听商城返回的产品选择
    const eventChannel = this.getOpenerEventChannel && this.getOpenerEventChannel();
    if (eventChannel) {
      eventChannel.on && eventChannel.on('selectProduct', (data) => {
        this.setData({ selectedProduct: data.product });
      });
    }
  },
  onSearchInput(e) {
    this.setData({ searchValue: e.detail.value });
  },
  onSearch() {
    const keyword = this.data.searchValue.trim();
    if (!keyword) {
      this.setData({ outletList: this.data.allOutlets });
      return;
    }
    const filtered = this.data.allOutlets.filter(item =>
      item.name.includes(keyword) || item.address.includes(keyword)
    );
    this.setData({ outletList: filtered });
  },
  onCall(e) {
    const index = e.currentTarget.dataset.index;
    const phone = this.data.outletList[index].phone;
    wx.makePhoneCall({ phoneNumber: phone });
  },
  onMap(e) {
    const index = e.currentTarget.dataset.index;
    const outlet = this.data.outletList[index];
    wx.openLocation({
      latitude: outlet.latitude,
      longitude: outlet.longitude,
      name: outlet.name,
      address: outlet.address
    });
  },
  onSelectProduct() {
    console.log('点击了服务产品');
    wx.navigateTo({
      url: '/pages/shopSelect/shopSelect',
      events: {
        selectProduct: (data) => {
          this.setData({ selectedProduct: data.product });
        }
      },
      fail: (err) => {
        console.error('跳转失败', err);
        wx.showToast({ title: '跳转失败', icon: 'none' });
      }
    });
  },
  onSelectRegion() {
    console.log('选择服务区域');
    wx.chooseLocation({
      success: (res) => {
        this.setData({ selectedRegion: res.address || res.name });
      }
    });
  },
  autoLocate() {
    wx.getLocation({
      type: 'wgs84',
      success: () => {
        this.setData({ selectedRegion: '定位成功' });
      },
      fail: () => {
        this.setData({ selectedRegion: '定位失败，请手动选择' });
      }
    });
  },
  onQuery() {
    const { selectedProduct, selectedRegion, allOutlets } = this.data;

    if (!selectedProduct || selectedProduct === '请选择') {
      wx.showToast({ title: '请选择服务产品', icon: 'none' });
      return;
    }
    if (!selectedRegion || selectedRegion === '请选择服务区域' || selectedRegion === '定位失败，请手动选择') {
      wx.showToast({ title: '请选择服务区域', icon: 'none' });
      return;
    }

    // 过滤网点数据
    const filteredOutlets = allOutlets.filter(outlet => {
      // 简单匹配产品和区域，实际中可能需要更复杂的匹配逻辑，例如模糊匹配区域
      return outlet.product === selectedProduct && outlet.region.includes(selectedRegion.split('市')[1] || selectedRegion);
    });

    this.setData({
      outletList: filteredOutlets
    });

    if (filteredOutlets.length === 0) {
      wx.showToast({ title: '未找到相关网点', icon: 'none' });
    } else {
      wx.showToast({ title: '查询成功', icon: 'success' });
      // 可以在这里进一步处理，例如跳转到展示网点列表的页面
    }
  },
  onShow() {
    const product = wx.getStorageSync('selectedProduct');
    if (product) {
      this.setData({ selectedProduct: product });
      wx.removeStorageSync('selectedProduct');
    }
  },
}); 